package com.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.entity.Orders;
import com.entity.Pingjia;
import com.entity.Tour;
import com.entity.User;
import com.entity.Zixun;
import com.service.OrdersService;
import com.service.PingjiaService;
import com.service.TourService;
import com.service.UserService;
import com.service.ZixunService;
import com.util.Arith;
import com.util.Util;



@Controller
@RequestMapping("/")
public class IndexController {

	@Resource
	private UserService userService;
	
	@Resource
	private TourService tourService;

	@Resource
	private ZixunService zixunService;
	
	@Resource
	private OrdersService ordersService;
	
	@Resource
	private PingjiaService pingjiaService;
	
	// 获取输出对象
	public PrintWriter getPrintWriter(HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return writer;
	}
	
	
	//网站首页
	@RequestMapping("/index.do")
	public String index(HttpServletRequest request,HttpServletResponse response,String pagenum,String biaoti,String type,String tuijian,String city){
		
		String url = "index.do";//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_tour where  ");
		
		//查询条件返回页面
		if (city != null && !"".equals(city)) {

			sb.append(" city like '%"+city+"%' ");
			sb.append(" and ");
			
			request.setAttribute("city", city);
		}
		
		if (biaoti != null && !"".equals(biaoti)) {

			sb.append(" biaoti like '%"+biaoti+"%' ");
			sb.append(" and ");
			
			request.setAttribute("biaoti", biaoti);
		}
		
		if ("1".equals(tuijian)) {

			sb.append(" tuijian= '已推荐' ");
			sb.append(" and ");
			
			request.setAttribute("tuijian", 1);
		}
		
		if("1".equals(type)){
			sb.append(" 1=1 order by buys desc ");
		}else if("2".equals(type)){
			sb.append(" 1=1 order by vist desc ");
		}else{
			sb.append(" 1=1 order by id desc ");
		}

		String sql = sb.toString();

		//查询列表
		Map<String,List<Tour>> map = tourService.selectBeanMap(currentpage,10,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<Tour> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);
		

		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, url, " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
		
		return "index.jsp";

	}
	
	
	//旅游信息详情页面
	@RequestMapping("/tourshow.do")
	public String tourshow(HttpServletRequest request,int id){

		Tour bean = tourService.selectData(" select * from t_tour where id= "+id).get(0);
		
		bean.setVist(bean.getVist()+1);
		
		tourService.updateBean(bean);
		
		request.setAttribute("bean", bean);
		
		
		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
		
		return "tourshow.jsp";

	}
	
	
	
	//旅游资讯
	@RequestMapping("/zixunlist.do")
	public String zixunlist(HttpServletRequest request,HttpServletResponse response,String pagenum,String biaoti){
		
		
		String url = "zixunlist.do";//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_zixun where  ");
		
		//查询条件返回页面
		if (biaoti != null && !"".equals(biaoti)) {

			sb.append(" biaoti like '%"+biaoti+"%' ");
			sb.append(" and ");
			
			request.setAttribute("biaoti", biaoti);
		}
		
		sb.append(" 1=1 order by id desc ");
		
		String sql = sb.toString();
		

		//查询列表
		Map<String,List<Zixun>> map = zixunService.selectBeanMap(currentpage,15,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<Zixun> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);

		//查询按钮
		request.setAttribute("url", url);

		
		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
		
		return "zixunlist.jsp";

	}
	
	
	//旅游资讯详情页面
	@RequestMapping("/zixunshow.do")
	public String zixunshow(HttpServletRequest request,int id){

		Zixun bean = zixunService.selectData(" select * from t_zixun where id= "+id).get(0);
		
		bean.setVist(bean.getVist()+1);
		
		zixunService.updateBean(bean);
		
		request.setAttribute("bean", bean);
		
		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
		
		return "zixunshow.jsp";

	}
	
	
	//跳转到用户注册页面
	@RequestMapping("/register.do")
	public String register(HttpServletRequest request){

		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
		
		return "register.jsp";

	}
	
	
	//用户注册操作
	@RequestMapping("/register2.do")
	public void register2(HttpServletRequest request,HttpServletResponse response,User user){
		
		PrintWriter writer = this.getPrintWriter(response);

		User bean = userService.useryz(user.getUsername());
		
		if(bean!=null){
			
			writer.print("<script language=javascript>alert('该用户名已经存在，注册失败！');window.location.href='register.do';</script>");
		
			return;	
		}

		user.setCtime(Util.getTime());
		user.setRole(2);
		
		
		
		userService.insertBean(user);
		
		writer.print("<script language=javascript>alert('注册成功');window.location.href='login.do';</script>");
		

	}
	
	
	//跳转到用户登录页面
	@RequestMapping("/login.do")
	public String login(HttpServletRequest request){

		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
		
		return "login.jsp";

	}
	
	
	//用户登录操作
	@RequestMapping("/login2.do")
	public void login2(HttpServletRequest request,HttpServletResponse response,String username,String password){
		
		PrintWriter writer = this.getPrintWriter(response);
		
		
		User bean = userService.userlogin(username, password, 2);
		
	
		
		if(bean==null){
			
			writer.print("<script language=javascript>alert('用户名或者密码错误，登录失败！');window.location.href='login.do';</script>");
		
			
		}else{
			
			HttpSession session = request.getSession();
			session.setAttribute("qiantai", bean);
			
			writer.print("<script language=javascript>alert('登录成功');window.location.href='.';</script>");
			
		}

	}
	
	
	
	//安全退出操作
	@RequestMapping("/loginout.do")
	public void loginout(HttpServletRequest request,HttpServletResponse response){
		
		PrintWriter writer = this.getPrintWriter(response);
		
		HttpSession session = request.getSession();
		session.removeAttribute("qiantai");
		
		writer.print("<script language=javascript>alert('退出成功');window.location.href='login.do';</script>");
		

	}
	
	
	
	//跳转到修改密码页面
	@RequestMapping("/password.do")
	public String password(HttpServletRequest request){

		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
		
		
		return "password.jsp";

	}
	
	
	//修改密码操作
	@RequestMapping("/password2.do")
	public void password2(HttpServletRequest request,HttpServletResponse response,String password1,String password2){
		
		PrintWriter writer = this.getPrintWriter(response);
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("qiantai");
		
		
		User bean = userService.userlogin(user.getUsername(), password1, 2);
		
		if(bean==null){
			bean = userService.userlogin(user.getUsername(), password1, 3);
		}
		
		if(bean==null){
			
			writer.print("<script language=javascript>alert('原密码错误，修改失败！');window.location.href='password.do';</script>");
		
			
		}else{
			
			bean.setPassword(password2);
			
			userService.updateBean(bean);
			
			writer.print("<script language=javascript>alert('修改成功,请重新登录');window.location.href='loginout.do';</script>");
			
		}
		
		
	}
	
	
	//跳转到个人信息维护页面
	@RequestMapping("/userupdate.do")
	public String userupdate(HttpServletRequest request){

		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("qiantai");
		
		
		User bean = userService.selectBeanById(user.getId());
		
		request.setAttribute("bean", bean);
		
		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);

		return "userupdate.jsp";

	}
	
	
	//个人信息维护操作
	@RequestMapping("/userupdate2.do")
	public void userupdate2(HttpServletRequest request,HttpServletResponse response ){
		
		PrintWriter writer = this.getPrintWriter(response);
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("qiantai");
		
		String name = request.getParameter("name");
		String address = request.getParameter("address");
		String tel = request.getParameter("tel");
		
		User bean = userService.selectBeanById(user.getId());
		
		bean.setName(name);
		bean.setAddress(address);
		bean.setTel(tel);
		
		userService.updateBean(bean);
		
		writer.print("<script language=javascript>alert('修改成功');window.location.href='userupdate.do';</script>");


	}
	
	
	//跳转到报名参加，生成订单页面
	@RequestMapping("/ordersadd.do")
	public String ordersadd(HttpServletRequest request,HttpServletResponse response) {
		
		PrintWriter writer = this.getPrintWriter(response);
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("qiantai");
		
		if (user == null) {
			writer.print("<script  language='javascript'>alert('请先登录');window.location.href='login.do';</script>");
			return  null;
		}
		
		String tourid = request.getParameter("tourid");
		
		request.setAttribute("tourid", tourid);
		
		Tour tour = tourService.selectData(" select * from t_tour where id= "+tourid).get(0);
		
		request.setAttribute("bean", tour);
		
		request.setAttribute("uu", user);
		
		
		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
	
		return "ordersadd.jsp";
		
	}
	
	
	//报名参加，生成订单操作
	@RequestMapping("/ordersadd2.do")
	public void ordersadd2(HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		PrintWriter writer = this.getPrintWriter(response);
	
		String remark = request.getParameter("remark");
		String renshu = request.getParameter("renshu");
		String tel = request.getParameter("tel");
		String name = request.getParameter("name");
		String tourid = request.getParameter("tourid");
		String ordersid = Util.getTime2();	
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("qiantai");
		
		User uu = userService.selectBeanById(user.getId());
		
		Tour tour = tourService.selectData(" select * from t_tour where id= "+tourid).get(0);
		
		double totalprice = Arith.mul(tour.getPrice(), Integer.parseInt(renshu));
		
		Orders bean = new Orders();;
		
		bean.setCtime(Util.getTime());
		bean.setName(name);
		bean.setOrdersid(ordersid);
		bean.setPrice(tour.getPrice());
		bean.setRemark(remark);
		bean.setRenshu(Integer.parseInt(renshu));
		bean.setStaus("处理中");
		bean.setTel(tel);
		bean.setTotalprice(totalprice);
		bean.setTourbiaoti(tour.getBiaoti());
		bean.setTourid(Integer.parseInt(tourid));
		bean.setUsername(uu.getUsername());

		ordersService.insertBean(bean);
		
		
		Pingjia pj = new Pingjia();
		
		pj.setOrdersid(ordersid);
		pj.setTourid(bean.getTourid());
		pj.setTourbiaoti(tour.getBiaoti());
		pj.setUsername(uu.getUsername());
		pj.setName(uu.getName());
		pj.setStatus("未评价");

		pingjiaService.insertBean(pj);
		
		
		writer.print("<script  language='javascript'>alert('操作成功');window.location.href='orderslist.do'; </script>");
		
	
	}
	
	
	//帖子列表
	@RequestMapping("/orderslist.do")
	public String orderslist(HttpServletRequest request,HttpServletResponse response,String pagenum,String ordersid){
		
		
		String url = "orderslist.do";//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_orders where  ");
		
		//查询条件返回页面
		if (ordersid != null && !"".equals(ordersid)) {

			sb.append(" ordersid like '%"+ordersid+"%' ");
			sb.append(" and ");
			
			request.setAttribute("ordersid", ordersid);
		}
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("qiantai");
		
		sb.append(" username='"+user.getUsername()+"' order by id desc ");
		
		String sql = sb.toString();
		

		//查询列表
		Map<String,List<Orders>> map = ordersService.selectBeanMap(currentpage,15,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<Orders> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);

		//查询按钮
		request.setAttribute("url", url);

		
		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
		
		
		return "orderslist.jsp";

	}
	
	
	//取消订单操作
	@RequestMapping("/ordersupdate.do")
	public void ordersupdate(HttpServletResponse response,int id){

		Orders bean = ordersService.selectData(" select * from t_orders where id= "+id).get(0); 
		
		bean.setStaus("已取消");

		ordersService.updateBean(bean);

		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='orderslist.do';</script>");
	}
	
	
	
	//删除订单操作
	@RequestMapping("/ordersdelete.do")
	public void ordersdelete(HttpServletResponse response,int id){

		ordersService.deleteBean(id);

		this.getPrintWriter(response).print("<script language=javascript>alert('操作成功');window.location.href='orderslist.do';</script>");
	}
	
	
	//订单详情页面
	@RequestMapping("/ordersshow.do")
	public String ordersshow(HttpServletRequest request,int id){

		Orders bean = ordersService.selectData(" select * from t_orders where id= "+id).get(0);
		
		
		request.setAttribute("bean", bean);
		
		Map<String,List<Orders>> maplist = ordersService.selectBeanMap(1, 15, "", " select * from t_orders order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Orders> orderslist = maplist.get(pagerinfo2);
		
		request.setAttribute("orderslist", orderslist);
		
		return "ordersshow.jsp";

	}
	
	
	
	//跳转到评价页面
	@RequestMapping("/pingjiaadd.do")
	public String pingjiaadd(HttpServletRequest request,HttpServletResponse response) {
		
		String ordersid = request.getParameter("ordersid");
		
		Pingjia bean = pingjiaService.selectData(" select * from t_pingjia where ordersid= '"+ordersid+"'").get(0);
		
		if(bean==null){
			PrintWriter writer = this.getPrintWriter(response);
			
			writer.print("<script  language='javascript'>alert('该评价已经被管理删除');window.location.href='orderslist.do'; </script>");
			return null;
		}
		
		if("未评价".equals(bean.getStatus())){
			request.setAttribute("bean", bean);
			
			
			Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
			String pagerinfo2 = maplist.keySet().iterator().next();
			List<Zixun> zixunlist = maplist.get(pagerinfo2);
			
			request.setAttribute("zixunlist", zixunlist);
		
			return "pingjiaadd.jsp";
		}else{
			request.setAttribute("bean", bean);
			
			
			Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
			String pagerinfo2 = maplist.keySet().iterator().next();
			List<Zixun> zixunlist = maplist.get(pagerinfo2);
			
			request.setAttribute("zixunlist", zixunlist);
		
			return "pingjiashow.jsp";
		}
		
		
		
	}
	
	
	//评价操作
	@RequestMapping("/pingjiaadd2.do")
	public void pingjiaadd2(HttpServletRequest request,HttpServletResponse response,int id) throws IOException{
		
		PrintWriter writer = this.getPrintWriter(response);
	
		String pcontent = request.getParameter("pcontent");
		
		Pingjia  bean = pingjiaService.selectData(" select * from t_pingjia where id= "+id).get(0);
		
		bean.setCtime(Util.getRiqi());
		bean.setPcontent(pcontent);
		bean.setStatus("已评价");
		

		pingjiaService.updateBean(bean);
		

		writer.print("<script  language='javascript'>alert('操作成功');window.location.href='orderslist.do'; </script>");
		
	
	}
	
	
	
	//旅游评价列表
	@RequestMapping("/pingjialist.do")
	public String pingjialist(HttpServletRequest request,HttpServletResponse response,String pagenum,String tourid){
		
		
		String url = "pingjialist.do?tourid="+tourid;//当前访问的地址
		
		//默认第一页
		int currentpage = 1;
		//获取当前页
		if (pagenum != null) {
			currentpage = Integer.parseInt(pagenum);
		}
		
		//组装查询的sql语句
		StringBuffer sb = new StringBuffer();
		
		sb.append(" select * from t_pingjia where  ");
		
		
		
		sb.append(" tourid="+tourid+" order by id desc ");
		
		String sql = sb.toString();
		

		//查询列表
		Map<String,List<Pingjia>> map = pingjiaService.selectBeanMap(currentpage,15,url,sql);
		String pagerinfo = map.keySet().iterator().next();
		List<Pingjia> list = map.get(pagerinfo);

		//列表返回页面
		request.setAttribute("list", list);

		//分页信息返回页面
		request.setAttribute("pagerinfo", pagerinfo);

		//查询按钮
		request.setAttribute("url", url);

		
		Map<String,List<Zixun>> maplist = zixunService.selectBeanMap(1, 15, "", " select * from t_zixun order by id desc ");
		String pagerinfo2 = maplist.keySet().iterator().next();
		List<Zixun> zixunlist = maplist.get(pagerinfo2);
		
		request.setAttribute("zixunlist", zixunlist);
		
		
		return "pingjialist.jsp";

	}
	
	
}












